#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=200;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
char str[maxn][maxn];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
struct Node{
    LL x,y;LL dis;
};
bool vis[maxn][maxn];
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,m;cin>>n>>m;
  LL sx,sy,fx,fy;cin>>sx>>sy>>fx>>fy;
  for(LL i=1;i<=n;i++){
    for(LL j=1;j<=m;j++){
        cin>>str[i][j];
    }
  }
  queue<Node>que;
  que.push({sx,sy,0});
  vis[sx][sy]=1;
  LL dis=0;
  while(!que.empty()){
     Node now=que.front();que.pop();
     if(now.x==fx&&now.y==fy){
        dis=now.dis;
        break;
     }
     for(LL i=0;i<4;i++){
        LL nx=now.x+dx[i];
        LL ny=now.y+dy[i];
        if(str[nx][ny]=='#'||nx<1||nx>n||ny<1||ny>m||vis[nx][ny]) continue;
        vis[nx][ny]=1;
        que.push({nx,ny,now.dis+1});
     }
  }
  if(!vis[fx][fy]) cout<<"-1"<<"\n";
  else cout<<dis*100<<"\n";
return 0;
}
